package com.flink.flinkdemo.demo;

import com.flink.flinkdemo.flat.MyFlatMap;
import com.flink.flinkdemo.reduce.MyReduceFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author zhangy on 2021/1/2
 * @description: add you description
 */
public class ReduceDemo {


    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> source = env.socketTextStream("localhost", 9001);

        SingleOutputStreamOperator<PojoDemo> pojoOpertor = source.flatMap(new FlatMapFunction<String, PojoDemo>() {
            @Override
            public void flatMap(String value, Collector<PojoDemo> out) throws Exception {
                String[] words = value.split(" ");
                out.collect(new PojoDemo(words[0], Integer.valueOf(words[1])));
            }
        });

        pojoOpertor.print();

        env.execute();
    }


    public static class PojoDemo{
        private String name;
        private Integer age;

        public PojoDemo() {
        }

        public PojoDemo(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "PojoDemo{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}